﻿
using System.Collections.Generic;
using com.loto.Common.DBUtility;
using System;
using com.huigo.Framework.Model;
namespace com.huigo.Framework.BLL
{
    public class ProductInfo
    {
        public static bool AddProductInfo(Model.ProductInfo productInfo)
        {
            return SqlServerDal.ProductInfo.Insert(productInfo, null);
        }

        public static List<Model.ProductInfo> GetProductList(string proName, int pageSize, int pageIndex)
        {
            List<string> fields = new List<string>() 
             {
                "PRODUCT_ID",
                "PRODUCT_NAME",
                "PRODUCT_SHORT_NAME",
                "PRODUCT_PRICE",
                "PRODUCT_SETTLE_PRICE",
                "COMPANY_SHORT_NAME",
                "SUP_NAME",
                "TYPE_NAME"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_INS_COMPANY]","COMPANY_ID=PRODUCT_INS_FID"),
                new JoinStatement("LEFT","[HG_INS_TYPE]","TYPE_ID=PRODUCT_TYPE_FID"),
                new JoinStatement("LEFT","[HG_SUP_INFO]","SUP_ID=PRODUCT_SUP_FID"),
            };
            List<OrderByStatement> OrderByStatement = new List<OrderByStatement>() 
             {
                 new OrderByStatement("PRODUCT_STATUS",false),
                 new OrderByStatement("PRODUCT_RECOM_INDEX",true)
             };
            List<WhereStatement> whereList = null;
            if (!string.IsNullOrEmpty(proName))
            {
                whereList = new List<WhereStatement>()
                {
                    new WhereStatement("PRODUCT_NAME","LIKE","'%"+proName+"%'","OR"),
                    new WhereStatement("PRODUCT_SHORT_NAME","LIKE","'%"+proName+"%'","")
               };
            }
            List<Model.ProductInfo> productList = null;
            int recordCount = 0;
            int pageCount = 0;
            productList = SqlServerDal.ProductInfo.GetItemsByProc(fields, joinList, whereList, OrderByStatement, "[PROC_PAGINATION_OUT]", pageSize, pageIndex, out recordCount, out pageCount, null);
            productList.ForEach(delegate(Model.ProductInfo info)
            {
                info.RecordCount = recordCount;
                info.PageCount = pageCount;
            });
            return productList;

        }

        public static List<Model.ProductInfo> GetProductListByInsType(int insType, int pageSize, int pageIndex)
        {
            List<string> fields = new List<string>() 
             {
                "PRODUCT_ID",
                "PRODUCT_NAME",
                "PRODUCT_SHORT_NAME",
                "PRODUCT_PRICE",
                "PRODUCT_SETTLE_PRICE",
                "COMPANY_SHORT_NAME",
                "COMPANY_LOGO",
                "SUP_NAME",
                "PRODUCT_INTRO",
                "PRODUCT_FEATURE",
                "PRODUCT_TIME_DESC"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_INS_COMPANY]","COMPANY_ID=PRODUCT_INS_FID"),
                new JoinStatement("LEFT","[HG_SUP_INFO]","SUP_ID=PRODUCT_SUP_FID"),
            };
            List<OrderByStatement> OrderByStatement = new List<OrderByStatement>() 
            {
                 new OrderByStatement("PRODUCT_RECOM_INDEX",true)
            };
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                 new WhereStatement("PRODUCT_TYPE_FID","=",insType.ToString(),"AND"),
                 new WhereStatement("PRODUCT_STATUS","=",((int)Dict.IsActive.Yes).ToString(),"")
            };
            List<Model.ProductInfo> productList = null;
            int recordCount = 0;
            int pageCount = 0;
            productList = SqlServerDal.ProductInfo.GetItemsByProc(fields, joinList, whereList, OrderByStatement, "[PROC_PAGINATION_OUT]", pageSize, pageIndex, out recordCount, out pageCount, null);
            productList.ForEach(delegate(Model.ProductInfo info)
            {
                info.RecordCount = recordCount;
                info.PageCount = pageCount;
            });
            return productList;

        }

        public static Model.ProductInfo GetProductInfoById(long id)
        {
            Model.ProductInfo productInfo = new Model.ProductInfo();
            productInfo.ProductId = id;
            List<string> fields = new List<string>() 
             {
                "[HG_PRODUCT_INFO].*",
                "COMPANY_SHORT_NAME",
                "SUP_NAME",
                "TYPE_NAME"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_INS_COMPANY]","COMPANY_ID=PRODUCT_INS_FID"),
                new JoinStatement("LEFT","[HG_INS_TYPE]","TYPE_ID=PRODUCT_TYPE_FID"),
                new JoinStatement("LEFT","[HG_SUP_INFO]","SUP_ID=PRODUCT_SUP_FID"),
            };

            return SqlServerDal.ProductInfo.GetItem(productInfo, fields, joinList, null);
        }

        public static bool UpdateProductInfo(long pid, Model.ProductInfo proInfo)
        {
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                    new WhereStatement("PRODUCT_ID","=",pid.ToString(),"")
            };
            return SqlServerDal.ProductInfo.Update(proInfo, whereList, null);
        }
        /// <summary>
        /// 根据产品ID查询产品详细
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static Model.ProductInfo GetProductInfoByProductId(long productId)
        {
            Model.ProductInfo productInfo = new com.huigo.Framework.Model.ProductInfo();
            List<string> fileds = new List<string>()
            {
                "PRODUCT_INS_FID,PRODUCT_NAME,PRODUCT_PRICE,PRODUCT_SETTLE_PRICE,PRODUCT_BEGIN_TIME,PRODUCT_END_TIME,PRODUCT_QUOTA,PRODUCT_AGE_MIN,PRODUCT_AGE_MAX,PRODUCT_INTRO,PRODUCT_DESC,PRODUCT_OTHERS,PRODUCT_SERVICE_DESC,PRODUCT_TIME_DESC,PRODUCT_FEATURE,PRODUCT_TYPE_FID,COMPANY_LOGO,COMPANY_ID,COMPANY_NAME"
            };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_INS_COMPANY]","COMPANY_ID=PRODUCT_INS_FID"),
            };
            productInfo.ProductId = productId;
            return SqlServerDal.ProductInfo.GetItem(productInfo, fileds, joinList, null);
        }

        public static Model.ProductInfo GetOrderProductInfoById(long id)
        {
            Model.ProductInfo productInfo = new Model.ProductInfo();
            productInfo.ProductId = id;
            List<string> fields = new List<string>() 
             {
                "[HG_PRODUCT_INFO].*",
                "COMPANY_EX",
                "COMPANY_NAME",
                "SUP_NAME",
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_INS_COMPANY]","COMPANY_ID=PRODUCT_INS_FID"),
                new JoinStatement("LEFT","[HG_SUP_INFO]","SUP_ID=PRODUCT_SUP_FID"),
            };

            return SqlServerDal.ProductInfo.GetItem(productInfo, fields, joinList, null);
        }
        /// <summary>
        /// 查询所有产品
        /// </summary>
        /// <returns></returns>
        public static List<Model.ProductInfo> GetProductInfoList()
        {
            List<string> fileds = new List<string>()
            {
                "PRODUCT_ID,PRODUCT_NAME"
            };
            return SqlServerDal.ProductInfo.GetItems(fileds, null, null, null, null, null);
        }

        public static List<Model.ProductInfo> GetProductListName()
        {
            List<string> fields = new List<string>() 
             {
                "PRODUCT_ID",
                "PRODUCT_NAME"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("LEFT","[HG_INS_COMPANY]","COMPANY_ID=PRODUCT_INS_FID"),
                new JoinStatement("LEFT","[HG_INS_TYPE]","TYPE_ID=PRODUCT_TYPE_FID"),
                new JoinStatement("LEFT","[HG_SUP_INFO]","SUP_ID=PRODUCT_SUP_FID"),
            };
            List<OrderByStatement> OrderByStatement = new List<OrderByStatement>() 
             {
                 new OrderByStatement("PRODUCT_NAME",true)
             };
            List<WhereStatement> whereList = new List<WhereStatement>();
            whereList.Add(new WhereStatement("PRODUCT_STATUS", "=", ((int)Dict.IsActive.Yes).ToString(), ""));
            List<Model.ProductInfo> productList = null;
            productList = SqlServerDal.ProductInfo.GetItems(fields, whereList, OrderByStatement, null);
            return productList;

        }
        public static List<Model.ProductInfo> GetProductListIndex(int type)
        {
            List<string> fileds = new List<string>()
            {
                "top 4 PRODUCT_ID,PRODUCT_TYPE_FID,PRODUCT_INS_FID,PRODUCT_SUP_FID,PRODUCT_NAME,PRODUCT_SHORT_NAME,COMPANY_LOGO"
            };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("left","HG_INS_COMPANY","PRODUCT_INS_FID=COMPANY_ID")
            };
            List<OrderByStatement> orderList = new List<OrderByStatement>();
            if (type == 1)
            {
                orderList.Add(new OrderByStatement("PRODUCT_BEGIN_TIME", false));
            }
            if (type == 2)
            {
                orderList.Add(new OrderByStatement("PRODUCT_RECOM_INDEX", true));
            }
            return SqlServerDal.ProductInfo.GetItems(fileds, joinList, null, orderList, null, null);
        }
    }
}
